package mini.hug;

import static bogus.core.BogusCore.egl;

import java.awt.Point;
import java.util.ArrayList;

import owg.util.Calc;
import owg.util.opengl.BlendMode;
import owg.util.opengl.ColorF;
import owg.util.opengl.Tex;
import world.Sound;
import graphics.Shape;
import hubol.Entity;
import hubol.Sprite;

public class Generate extends Entity{
	private final ArrayList<Point> list = new ArrayList<Point>();
	
	private float[] values = new  float[]{69, 420, 169, 75};
	private ColorF[] colors = new ColorF[]{ColorF.BOGUSBLUE, ColorF.GRAY, ColorF.ORANGE, ColorF.MAGENTA};
	
	private double previous;
	private double progress;
	private boolean wait;
	
	private int messaged;
	private final Messageable[] messageables = craft("bogus", "birgus", "borgus", "flower", "heart", "hug", "lightning", "scorch", "speech");
	private ArrayList<Message> messageList = new ArrayList<Message>();
	private final Sprite circle = new Sprite("circle"), message = new Sprite("message");
	
	private int grow = 1;

	public Generate() {
		super(432, 243, 1);
		sprite = new Sprite("generate");
		previous = 0;
		progress = 0;
		messaged = 0;
		alarm[0] = 25 + (int)Calc.random(90);
		
		setDepth(-100);
	}
	
	private Messageable[] craft(String... s){
		Messageable[] out = new Messageable[s.length];
		for (int i=0; i<s.length; i++)
			out[i] = new Messageable(new Sprite(s[i]));
		return out;
	}
	
	private class Messageable{
		private final Sprite sprite;
		private final float scale;
		private final double animationMultiplier;
		
		public Messageable(Sprite s){
			sprite = s;
			Tex t = s.texture;
			scale = (float)(100.0 / Math.max(t.height, t.width));
			animationMultiplier = 16 + Calc.random(32);
		}
		
		public void render(double x, double y, Shake s){
			message.render(0, x + s.x, y + s.y, .5f, .5f, (float)(s.z / 16.0), ColorF.WHITE);
			sprite.render((progress * animationMultiplier) % sprite.length, x + s.x, y + s.y, scale, scale, (float)(s.z / 16.0), ColorF.WHITE);
		}
	}
	
	private class Message{
		private final Messageable message;
		private double x, y;
		
		public Message(Messageable m){
			while (x < 400 && y < 260){
				x = 125 + Calc.random(614);
				y = 85 + Calc.random(316);
			}
			message = m;
		}
		
		public void render(Shake s){
			message.render(x, y, s);
		}
	}
	
	public void alarmEvent(int i){
		wait = !wait;
		alarm[0] = 25 + (int)Calc.random(90);
		if (wait)
			alarm[0] *= 1.3;
	}
	
	public void step(){
		if (!wait){
			Sound.playPitched("progress");
			progress += .0075;
			
			if (canGenerate(.00694235 + ((1 + messaged) * .08)) && messaged < messageables.length){
				messageList.add(new Message(messageables[messaged]));
				if (messageList.size() > 2)
					messageList.remove(0);
				messaged++;
			}
			
			borgus(.4, 1);
			borgus(.45, 2);
			borgus(.55, 3);
			borgus(.576, 4);
			borgus(.6, 5);
			borgus(.65, 6);
			borgus(.78, 7);
			borgus(.9, 8);
			
			generateFlower(.01);
			generateFlower(.05);
			generateFlower(.09);
			generateFlower(.12);
			generateFlower(.16);
			generateFlower(.165);
			generateFlower(.17);
			generateFlower(.22);
			generateFlower(.35);
			generateFlower(.55);
			generateFlower(.70);
			generateFlower(.88);
			generateFlower(.99);
			
			generateFlower(.3);
			generateFlower(.5);
			generateFlower(.7);
			generateFlower(.9);
			generateFlower(.11);
			generateFlower(.21);
			generateFlower(.41);
			generateFlower(.65);
			generateFlower(.35);
			generateFlower(.27);
			
			double i = .01;
			while (i < 1){
				generateFlower(i);
				i += .011;
				i *= 1.0011;
				if (i > .5)
					i -= .00123;
			}

			if (progress >= 1){
				Hug.me.addBogus();
				progress = 1;
				destroy();
			}
		}
		alarmStep();
	}
	
	public void borgus(double need, int id){
		if (previous >= need && Hug.me.borgus.size() < id)
			Hug.me.addBorgus();
	}
	
	public void generateFlower(double need){
		if (canGenerate(need)){
			new Flower(generatePoint());
			Sound.playPitched("grow" + grow);
			grow++;
			if (grow > 3)
				grow = 1;
		}
	}
	
	public Point generatePoint(){
		Point p = null;
		while (p == null/* || !screen.contains(p)*/ || !canPlace(p)){
			double dir = Calc.random(360);
			double dist = Calc.random(350);
			p = new Point(432 + (int)Calc.dirX(100 + dist, dir), 243 + (int)Calc.dirY(100 + (dist * .9), dir));
		}
		
		list.add(p);
		return p;
	}
	
	public boolean canPlace(Point p){
		for (Point q : list)
			if (q.distance(p) < 42)
				return false;
		
		for (int i=0; i<4; i++)
			values[i] = (float)Math.max(0, Calc.approach(values[i], (values[i] + Calc.rangedRandom(35)) * (1 + Calc.rangedRandom(.3)), 4));

		return true;
	}
	
	public boolean canGenerate(double need){
		if (previous < need && progress >= need){
			previous = progress;
			return true;
		}
		return false;
	}
	
	public void render(){
		int i = 0;
		for (Message m : messageList){
			m.render(Hug.me.s[2 + i]);
			i++;
		}
		
		sprite.render(0, x + Hug.me.s[0].x, y + Hug.me.s[0].y, .5f, .5f, (float)(Hug.me.s[0].z / 16.0), ColorF.WHITE);
		sprite.render(1 + (4.99 * progress), x + Hug.me.s[0].x, y + Hug.me.s[0].y, .5f, .5f, (float)(Hug.me.s[0].z / 16.0), ColorF.WHITE);
		
		circle.render(0, 150 + Hug.me.s[1].x, 150 + Hug.me.s[1].y, .416f, .416f, (float)(Hug.me.s[1].z / 20.0), ColorF.WHITE);
		egl.disableTexture2D();
		egl.setBlendMode(BlendMode.MULTIPLY);
		Shape.drawPieChart((float)(150 + Hug.me.s[1].x), (float)(150 + Hug.me.s[1].y), 90, 30, (float)(Hug.me.s[1].z / 20.0), values, colors);
		//circle.render(0, 150 + t.x, 150 + t.y, .316f, .316f, (float)(t.z / 20.0), ColorF.WHITE);
		//egl.setColor(ColorF.WHITE);
		//Shape.drawCircle((float)(150 + t.x), (float)(150 + t.y), 70);
		egl.setBlendMode(BlendMode.NORMAL_PRE);
		//public static void drawPieChart(float x, float y, float radius, int steps, float angle, float[] values, ColorF... colors){
	}

}
